/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.Materiel;
import Controller.ReadFile;
import Controller.Salle;
import Model.MaterielDAO;
import Model.SalleDAO;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import javax.swing.JFileChooser;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Artorius
 */
public class jframeResultInv extends javax.swing.JFrame {

  private List<String> listCB;
  private Connection connect;
  
  
  public jframeResultInv(Connection connect) {
	initComponents();
        this.listCB = new ArrayList<>();
        this.connect = connect;
        Map<Integer,Salle> salles = new HashMap();
        SalleDAO salleDAO = new SalleDAO(connect);
        salles = salleDAO.findAll();
        for (Integer mapKey : salles.keySet()) {
          // utilise ici hashMap.get(mapKey) pour accéder aux valeurs
          cbSalle.addItem(salles.get(mapKey));
        }
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    jFileChooser1 = new javax.swing.JFileChooser();
    jButton1 = new javax.swing.JButton();
    jFileChooser3 = new javax.swing.JFileChooser();
    jScrollPane1 = new javax.swing.JScrollPane();
    jtEnTrop = new javax.swing.JTable();
    jLabel1 = new javax.swing.JLabel();
    jLabel2 = new javax.swing.JLabel();
    jLabel3 = new javax.swing.JLabel();
    jScrollPane2 = new javax.swing.JScrollPane();
    jTable2 = new javax.swing.JTable();
    jScrollPane3 = new javax.swing.JScrollPane();
    jtOK = new javax.swing.JTable();
    bFindFile = new javax.swing.JButton();
    jLabel4 = new javax.swing.JLabel();
    bComparer = new javax.swing.JButton();
    cbSalle = new javax.swing.JComboBox();

    jButton1.setText("jButton1");

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    jtEnTrop.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "CodeBarre", "Nom", "SalleBD"
      }
    ));
    jScrollPane1.setViewportView(jtEnTrop);

    jLabel1.setText("Equipement en trop ");

    jLabel2.setText("Equipement Manquant");

    jLabel3.setText("Equipement ok");

    jTable2.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "CodeBarre", "Nom"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.Long.class, java.lang.String.class
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }
    });
    jScrollPane2.setViewportView(jTable2);

    jtOK.setModel(new javax.swing.table.DefaultTableModel(
      new Object [][] {

      },
      new String [] {
        "CodeBarre", "Nom"
      }
    ) {
      Class[] types = new Class [] {
        java.lang.Long.class, java.lang.String.class
      };

      public Class getColumnClass(int columnIndex) {
        return types [columnIndex];
      }
    });
    jScrollPane3.setViewportView(jtOK);

    bFindFile.setText("Parcourir");
    bFindFile.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bFindFileActionPerformed(evt);
      }
    });

    jLabel4.setText("N° Salle");

    bComparer.setText("Comparer");
    bComparer.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bComparerActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1))
              .addGroup(layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(jLabel3)
                  .addComponent(jLabel2))))
            .addGap(0, 0, Short.MAX_VALUE))
          .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
          .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING)
          .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING)
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addGap(0, 0, Short.MAX_VALUE)
            .addComponent(jLabel4)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(cbSalle, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(bFindFile)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(bComparer)))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addGap(37, 37, 37)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel4)
          .addComponent(bFindFile)
          .addComponent(bComparer)
          .addComponent(cbSalle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addGap(18, 18, Short.MAX_VALUE)
        .addComponent(jLabel1)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(26, 26, 26)
        .addComponent(jLabel2)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(61, 61, 61)
        .addComponent(jLabel3)
        .addGap(18, 18, 18)
        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addGap(31, 31, 31))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void bFindFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bFindFileActionPerformed
	// TODO add your handling code here:
	//Create a file chooser
	final JFileChooser fc = new JFileChooser();

//In response to a button click:
	int returnVal = fc.showOpenDialog(bFindFile);
	if (returnVal == JFileChooser.APPROVE_OPTION) {
	  File file = fc.getSelectedFile();
	  listCB = ReadFile.lire(file.getAbsolutePath());
	  System.out.println(listCB);
	} else {
	  System.out.println("Open command cancelled by user.");
	}
  }//GEN-LAST:event_bFindFileActionPerformed

    private void bComparerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bComparerActionPerformed

        Map<Materiel,Integer> lesMat = new HashMap();
        lesMat = new MaterielDAO(connect).materielEstDansSalle(this.listCB,((Salle)this.cbSalle.getSelectedItem()).getIdS());
        DefaultTableModel dtm = (DefaultTableModel)this.jtOK.getModel();
        dtm.getDataVector().removeAllElements();
        DefaultTableModel dtm2 = (DefaultTableModel)this.jtEnTrop.getModel();
        dtm2.getDataVector().removeAllElements();
        
        for(Object o : lesMat.keySet())
        {
            if(lesMat.get(o) == 0)
            {
                Vector v = new Vector();
                v.add(((Materiel)o).getNumCB());
                v.add(((Materiel)o).getNom());
                dtm.addRow(v);
            }
            else
            {
                Vector v = new Vector();
                v.add(((Materiel)o).getNumCB());
                v.add(((Materiel)o).getNom());
                dtm2.addRow(v);
            }
        }
        Map<Materiel,Integer> lesMatManquant = new HashMap();
        lesMatManquant = new MaterielDAO(connect).materielEstDansTxt(this.listCB, ((Salle)this.cbSalle.getSelectedItem()).getIdS());
        DefaultTableModel dtm3 = (DefaultTableModel)this.jTable2.getModel();
        dtm3.getDataVector().removeAllElements();
        
        for(Object o : lesMatManquant.keySet())
        {
            Vector v = new Vector();
            v.add(((Materiel)o).getNumCB());
            v.add(((Materiel)o).getNom());
            dtm3.addRow(v);
        }
        
        
    }//GEN-LAST:event_bComparerActionPerformed
  /**
   * @param args the command line arguments
   */
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton bComparer;
  private javax.swing.JButton bFindFile;
  private javax.swing.JComboBox cbSalle;
  private javax.swing.JButton jButton1;
  private javax.swing.JFileChooser jFileChooser1;
  private javax.swing.JFileChooser jFileChooser3;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JScrollPane jScrollPane3;
  private javax.swing.JTable jTable2;
  private javax.swing.JTable jtEnTrop;
  private javax.swing.JTable jtOK;
  // End of variables declaration//GEN-END:variables
}
